using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Modules.DOCUMENT.Components
{
    public partial class Item
    {
        #region Private members.

        protected int _ID;
        protected int _CategoryID;
        protected int _TypeID;
        protected int _PublisherID;
        protected string _Code = string.Empty;
        protected string _Summary = string.Empty;
        protected DateTime _PublishedDate = new DateTime(1753, 1, 1);
        protected DateTime _AvailableDate = new DateTime(1753, 1, 1);
        protected string _SignedBy = string.Empty;
        protected string _CreatedBy = string.Empty;
        protected DateTime _CreatedDate = new DateTime(1753, 1, 1);

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public int CategoryID
        {
            set { this._CategoryID = value; }
            get { return this._CategoryID; }
        }

        public int TypeID
        {
            set { this._TypeID = value; }
            get { return this._TypeID; }
        }

        public int PublisherID
        {
            set { this._PublisherID = value; }
            get { return this._PublisherID; }
        }

        public string Code
        {
            set { this._Code = value; }
            get { return this._Code; }
        }

        public string Summary
        {
            set { this._Summary = value; }
            get { return this._Summary; }
        }

        public DateTime PublishedDate
        {
            set { this._PublishedDate = value; }
            get { return this._PublishedDate; }
        }

        public DateTime AvailableDate
        {
            set { this._AvailableDate = value; }
            get { return this._AvailableDate; }
        }

        public string SignedBy
        {
            set { this._SignedBy = value; }
            get { return this._SignedBy; }
        }

        public string CreatedBy
        {
            set { this._CreatedBy = value; }
            get { return this._CreatedBy; }
        }

        public DateTime CreatedDate
        {
            set { this._CreatedDate = value; }
            get { return this._CreatedDate; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Item Load(int id)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_Load]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, id);
            Item entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Item();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TypeID"))) entity.TypeID = reader.GetInt32(reader.GetOrdinal("TypeID"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublisherID"))) entity.PublisherID = reader.GetInt32(reader.GetOrdinal("PublisherID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Code"))) entity.Code = reader.GetString(reader.GetOrdinal("Code"));
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) entity.Summary = reader.GetString(reader.GetOrdinal("Summary"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublishedDate"))) entity.PublishedDate = reader.GetDateTime(reader.GetOrdinal("PublishedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("AvailableDate"))) entity.AvailableDate = reader.GetDateTime(reader.GetOrdinal("AvailableDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("SignedBy"))) entity.SignedBy = reader.GetString(reader.GetOrdinal("SignedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
            }
            reader.Close();
            return entity;
        }

        //---------------------------------------------------------------------------------------------
        public static List<Item> SelectCollectionAll()
        {
            List<Item> collection = new List<Item>();
            SqlDataReader reader = (SqlDataReader)SelectReaderAll();
            while (reader.Read())
            {
                Item entity = new Item();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TypeID"))) entity.TypeID = reader.GetInt32(reader.GetOrdinal("TypeID"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublisherID"))) entity.PublisherID = reader.GetInt32(reader.GetOrdinal("PublisherID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Code"))) entity.Code = reader.GetString(reader.GetOrdinal("Code"));
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) entity.Summary = reader.GetString(reader.GetOrdinal("Summary"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublishedDate"))) entity.PublishedDate = reader.GetDateTime(reader.GetOrdinal("PublishedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("AvailableDate"))) entity.AvailableDate = reader.GetDateTime(reader.GetOrdinal("AvailableDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("SignedBy"))) entity.SignedBy = reader.GetString(reader.GetOrdinal("SignedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        public static List<Item> SelectCollectionDynamic(string whereCondition, string orderByExpression)
        {
            List<Item> collection = new List<Item>();

            SqlDataReader reader = (SqlDataReader)SelectReaderDynamic(whereCondition, orderByExpression);
            while (reader.Read())
            {
                Item entity = new Item();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TypeID"))) entity.TypeID = reader.GetInt32(reader.GetOrdinal("TypeID"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublisherID"))) entity.PublisherID = reader.GetInt32(reader.GetOrdinal("PublisherID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Code"))) entity.Code = reader.GetString(reader.GetOrdinal("Code"));
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) entity.Summary = reader.GetString(reader.GetOrdinal("Summary"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublishedDate"))) entity.PublishedDate = reader.GetDateTime(reader.GetOrdinal("PublishedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("AvailableDate"))) entity.AvailableDate = reader.GetDateTime(reader.GetOrdinal("AvailableDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("SignedBy"))) entity.SignedBy = reader.GetString(reader.GetOrdinal("SignedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        // Select by foreign key return collection		
        public static IList<Item> SelectCollectionBy_PublisherID(int publisherID)
        {
            List<Item> collection = new List<Item>();
            SqlDataReader reader = (SqlDataReader)SelectReaderBy_PublisherID(publisherID);
            while (reader.Read())
            {
                Item entity = new Item();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TypeID"))) entity.TypeID = reader.GetInt32(reader.GetOrdinal("TypeID"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublisherID"))) entity.PublisherID = reader.GetInt32(reader.GetOrdinal("PublisherID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Code"))) entity.Code = reader.GetString(reader.GetOrdinal("Code"));
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) entity.Summary = reader.GetString(reader.GetOrdinal("Summary"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublishedDate"))) entity.PublishedDate = reader.GetDateTime(reader.GetOrdinal("PublishedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("AvailableDate"))) entity.AvailableDate = reader.GetDateTime(reader.GetOrdinal("AvailableDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("SignedBy"))) entity.SignedBy = reader.GetString(reader.GetOrdinal("SignedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                collection.Add(entity);
            }
            reader.Close();
            return collection;
        }
        //---------------------------------------------------------------------------------------------
        public static IList<Item> SelectCollectionBy_TypeID(int typeID)
        {
            List<Item> collection = new List<Item>();
            SqlDataReader reader = (SqlDataReader)SelectReaderBy_TypeID(typeID);
            while (reader.Read())
            {
                Item entity = new Item();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TypeID"))) entity.TypeID = reader.GetInt32(reader.GetOrdinal("TypeID"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublisherID"))) entity.PublisherID = reader.GetInt32(reader.GetOrdinal("PublisherID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Code"))) entity.Code = reader.GetString(reader.GetOrdinal("Code"));
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) entity.Summary = reader.GetString(reader.GetOrdinal("Summary"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublishedDate"))) entity.PublishedDate = reader.GetDateTime(reader.GetOrdinal("PublishedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("AvailableDate"))) entity.AvailableDate = reader.GetDateTime(reader.GetOrdinal("AvailableDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("SignedBy"))) entity.SignedBy = reader.GetString(reader.GetOrdinal("SignedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                collection.Add(entity);
            }
            reader.Close();
            return collection;
        }
        //---------------------------------------------------------------------------------------------
        public static IList<Item> SelectCollectionBy_CategoryID(int categoryID)
        {
            List<Item> collection = new List<Item>();
            SqlDataReader reader = (SqlDataReader)SelectReaderBy_CategoryID(categoryID);
            while (reader.Read())
            {
                Item entity = new Item();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TypeID"))) entity.TypeID = reader.GetInt32(reader.GetOrdinal("TypeID"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublisherID"))) entity.PublisherID = reader.GetInt32(reader.GetOrdinal("PublisherID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Code"))) entity.Code = reader.GetString(reader.GetOrdinal("Code"));
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) entity.Summary = reader.GetString(reader.GetOrdinal("Summary"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublishedDate"))) entity.PublishedDate = reader.GetDateTime(reader.GetOrdinal("PublishedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("AvailableDate"))) entity.AvailableDate = reader.GetDateTime(reader.GetOrdinal("AvailableDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("SignedBy"))) entity.SignedBy = reader.GetString(reader.GetOrdinal("SignedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                collection.Add(entity);
            }
            reader.Close();
            return collection;
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectBy_PublisherID(int publisherID)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_SelectBy_PublisherID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@PublisherID", SqlDbType.Int, publisherID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------
        public static DataSet SelectBy_TypeID(int typeID)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_SelectBy_TypeID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@TypeID", SqlDbType.Int, typeID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------
        public static DataSet SelectBy_CategoryID(int categoryID)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_SelectBy_CategoryID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        // Select by foreign key return collection		
        public static IDataReader SelectReaderBy_PublisherID(int publisherID)
        {
            const string spName = "p_DOCUMENT_Item_SelectBy_PublisherID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@PublisherID", SqlDbType.Int, publisherID);

            return db.ExecuteReader(dbCommand);
        }
        //---------------------------------------------------------------------------------------------
        public static IDataReader SelectReaderBy_TypeID(int typeID)
        {
            const string spName = "p_DOCUMENT_Item_SelectBy_TypeID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@TypeID", SqlDbType.Int, typeID);

            return db.ExecuteReader(dbCommand);
        }
        //---------------------------------------------------------------------------------------------
        public static IDataReader SelectReaderBy_CategoryID(int categoryID)
        {
            const string spName = "p_DOCUMENT_Item_SelectBy_CategoryID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);

            return db.ExecuteReader(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int InsertItem(int categoryID, int typeID, int publisherID, string code, string summary, DateTime publishedDate, DateTime availableDate, string signedBy, string createdBy, DateTime createdDate)
        {
            Item entity = new Item();
            entity.CategoryID = categoryID;
            entity.TypeID = typeID;
            entity.PublisherID = publisherID;
            entity.Code = code;
            entity.Summary = summary;
            entity.PublishedDate = publishedDate;
            entity.AvailableDate = availableDate;
            entity.SignedBy = signedBy;
            entity.CreatedBy = createdBy;
            entity.CreatedDate = createdDate;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_Insert]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, CategoryID);
            db.AddInParameter(dbCommand, "@TypeID", SqlDbType.Int, TypeID);
            db.AddInParameter(dbCommand, "@PublisherID", SqlDbType.Int, PublisherID);
            db.AddInParameter(dbCommand, "@Code", SqlDbType.NVarChar, Code);
            db.AddInParameter(dbCommand, "@Summary", SqlDbType.NVarChar, Summary);
            db.AddInParameter(dbCommand, "@PublishedDate", SqlDbType.DateTime, PublishedDate.Year <= 1753 ? DBNull.Value : (object)PublishedDate);
            db.AddInParameter(dbCommand, "@AvailableDate", SqlDbType.DateTime, AvailableDate.Year <= 1753 ? DBNull.Value : (object)AvailableDate);
            db.AddInParameter(dbCommand, "@SignedBy", SqlDbType.NVarChar, SignedBy);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);
            db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year <= 1753 ? DBNull.Value : (object)CreatedDate);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return ID;
            }
            db.ExecuteNonQuery(dbCommand);
            ID = (int)db.GetParameterValue(dbCommand, "@ID");
            return ID;
        }

        //---------------------------------------------------------------------------------------------
        public static bool InsertCollection(IList<Item> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Item item in collection)
                        {
                            if (item.Insert(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at InsertCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert / Update methods.

        public static int InsertUpdateItem(int id, int categoryID, int typeID, int publisherID, string code, string summary, DateTime publishedDate, DateTime availableDate, string signedBy, string createdBy, DateTime createdDate)
        {
            Item entity = new Item();
            entity.ID = id;
            entity.CategoryID = categoryID;
            entity.TypeID = typeID;
            entity.PublisherID = publisherID;
            entity.Code = code;
            entity.Summary = summary;
            entity.PublishedDate = publishedDate;
            entity.AvailableDate = availableDate;
            entity.SignedBy = signedBy;
            entity.CreatedBy = createdBy;
            entity.CreatedDate = createdDate;
            return entity.InsertUpdate();
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate()
        {
            return this.InsertUpdate(null);
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate(SqlTransaction transaction)
        {
            const string spName = "p_DOCUMENT_Item_InsertUpdate";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, CategoryID);
            db.AddInParameter(dbCommand, "@TypeID", SqlDbType.Int, TypeID);
            db.AddInParameter(dbCommand, "@PublisherID", SqlDbType.Int, PublisherID);
            db.AddInParameter(dbCommand, "@Code", SqlDbType.NVarChar, Code);
            db.AddInParameter(dbCommand, "@Summary", SqlDbType.NVarChar, Summary);
            db.AddInParameter(dbCommand, "@PublishedDate", SqlDbType.DateTime, PublishedDate.Year == 1753 ? DBNull.Value : (object)PublishedDate);
            db.AddInParameter(dbCommand, "@AvailableDate", SqlDbType.DateTime, AvailableDate.Year == 1753 ? DBNull.Value : (object)AvailableDate);
            db.AddInParameter(dbCommand, "@SignedBy", SqlDbType.NVarChar, SignedBy);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);
            db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year == 1753 ? DBNull.Value : (object)CreatedDate);

            if (transaction != null)
            {
                return db.ExecuteNonQuery(dbCommand, transaction);
            }
            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------
        public static bool InsertUpdateCollection(IList<Item> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Item item in collection)
                        {
                            if (item.InsertUpdate(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);

                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }
        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int UpdateItem(int id, int categoryID, int typeID, int publisherID, string code, string summary, DateTime publishedDate, DateTime availableDate, string signedBy, string createdBy, DateTime createdDate)
        {
            Item entity = new Item();
            entity.ID = id;
            entity.CategoryID = categoryID;
            entity.TypeID = typeID;
            entity.PublisherID = publisherID;
            entity.Code = code;
            entity.Summary = summary;
            entity.PublishedDate = publishedDate;
            entity.AvailableDate = availableDate;
            entity.SignedBy = signedBy;
            entity.CreatedBy = createdBy;
            entity.CreatedDate = createdDate;
            return entity.Update();
        }

        //---------------------------------------------------------------------------------------------

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_Update]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, CategoryID);
            db.AddInParameter(dbCommand, "@TypeID", SqlDbType.Int, TypeID);
            db.AddInParameter(dbCommand, "@PublisherID", SqlDbType.Int, PublisherID);
            db.AddInParameter(dbCommand, "@Code", SqlDbType.NVarChar, Code);
            db.AddInParameter(dbCommand, "@Summary", SqlDbType.NVarChar, Summary);
            db.AddInParameter(dbCommand, "@PublishedDate", SqlDbType.DateTime, PublishedDate.Year == 1753 ? DBNull.Value : (object)PublishedDate);
            db.AddInParameter(dbCommand, "@AvailableDate", SqlDbType.DateTime, AvailableDate.Year == 1753 ? DBNull.Value : (object)AvailableDate);
            db.AddInParameter(dbCommand, "@SignedBy", SqlDbType.NVarChar, SignedBy);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);
            db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year == 1753 ? DBNull.Value : (object)CreatedDate);

            if (transaction != null)
            {
                return db.ExecuteNonQuery(dbCommand, transaction);
            }
            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------
        public static bool UpdateCollection(IList<Item> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Item item in collection)
                        {
                            if (item.Update(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at UpdateCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int DeleteItem(int id)
        {
            Item entity = new Item();
            entity.ID = id;

            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_Delete]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);

            if (transaction != null)
            {
                return db.ExecuteNonQuery(dbCommand, transaction);
            }
            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static int DeleteBy_PublisherID(int publisherID)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_DeleteBy_PublisherID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@PublisherID", SqlDbType.Int, publisherID);

            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static int DeleteBy_TypeID(int typeID)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_DeleteBy_TypeID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@TypeID", SqlDbType.Int, typeID);

            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static int DeleteBy_CategoryID(int categoryID)
        {
            const string spName = "[dbo].[p_DOCUMENT_Item_DeleteBy_CategoryID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);

            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static bool DeleteCollection(IList<Item> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Item item in collection)
                        {
                            if (item.Delete(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at DeleteCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }
        #endregion
    }
}